home *** CD-ROM | disk | FTP | other *** search
/ Introduction to 3D Game …ogramming with DirectX 12 / Introduction-to-3D-Game-Programming-with-DirectX-12.ISO / Code.Textures / Chapter 21 Ambient Occlusion / Ssao / Shaders / DrawNormals.hlsl < prev    next >
Encoding:
Text File  |  2016-03-02  |  2.5 KB  |  88 lines

  1. //***************************************************************************************
  2. // DrawNormals.hlsl by Frank Luna (C) 2015 All Rights Reserved.
  3. //***************************************************************************************
  4.  
  5. // Defaults for number of lights.
  6. #ifndef NUM_DIR_LIGHTS
  7.     #define NUM_DIR_LIGHTS 0
  8. #endif
  9.  
  10. #ifndef NUM_POINT_LIGHTS
  11.     #define NUM_POINT_LIGHTS 0
  12. #endif
  13.  
  14. #ifndef NUM_SPOT_LIGHTS
  15.     #define NUM_SPOT_LIGHTS 0
  16. #endif
  17.  
  18. // Include common HLSL code.
  19. #include "Common.hlsl"
  20.  
  21. struct VertexIn
  22. {
  23.     float3 PosL    : POSITION;
  24.     float3 NormalL : NORMAL;
  25.     float2 TexC    : TEXCOORD;
  26.     float3 TangentU : TANGENT;
  27. };
  28.  
  29. struct VertexOut
  30. {
  31.     float4 PosH     : SV_POSITION;
  32.     float3 NormalW  : NORMAL;
  33.     float3 TangentW : TANGENT;
  34.     float2 TexC     : TEXCOORD;
  35. };
  36.  
  37. VertexOut VS(VertexIn vin)
  38. {
  39.     VertexOut vout = (VertexOut)0.0f;
  40.  
  41.     // Fetch the material data.
  42.     MaterialData matData = gMaterialData[gMaterialIndex];
  43.     
  44.     // Assumes nonuniform scaling; otherwise, need to use inverse-transpose of world matrix.
  45.     vout.NormalW = mul(vin.NormalL, (float3x3)gWorld);
  46.     vout.TangentW = mul(vin.TangentU, (float3x3)gWorld);
  47.  
  48.     // Transform to homogeneous clip space.
  49.     float4 posW = mul(float4(vin.PosL, 1.0f), gWorld);
  50.     vout.PosH = mul(posW, gViewProj);
  51.     
  52.     // Output vertex attributes for interpolation across triangle.
  53.     float4 texC = mul(float4(vin.TexC, 0.0f, 1.0f), gTexTransform);
  54.     vout.TexC = mul(texC, matData.MatTransform).xy;
  55.     
  56.     return vout;
  57. }
  58.  
  59. float4 PS(VertexOut pin) : SV_Target
  60. {
  61.     // Fetch the material data.
  62.     MaterialData matData = gMaterialData[gMaterialIndex];
  63.     float4 diffuseAlbedo = matData.DiffuseAlbedo;
  64.     uint diffuseMapIndex = matData.DiffuseMapIndex;
  65.     uint normalMapIndex = matData.NormalMapIndex;
  66.     
  67.     // Dynamically look up the texture in the array.
  68.     diffuseAlbedo *= gTextureMaps[diffuseMapIndex].Sample(gsamAnisotropicWrap, pin.TexC);
  69.  
  70. #ifdef ALPHA_TEST
  71.     // Discard pixel if texture alpha < 0.1.  We do this test as soon 
  72.     // as possible in the shader so that we can potentially exit the
  73.     // shader early, thereby skipping the rest of the shader code.
  74.     clip(diffuseAlbedo.a - 0.1f);
  75. #endif
  76.  
  77.     // Interpolating normal can unnormalize it, so renormalize it.
  78.     pin.NormalW = normalize(pin.NormalW);
  79.     
  80.     // NOTE: We use interpolated vertex normal for SSAO.
  81.  
  82.     // Write normal in view space coordinates
  83.     float3 normalV = mul(pin.NormalW, (float3x3)gView);
  84.     return float4(normalV, 0.0f);
  85. }
  86.  
  87.  
  88.